package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsalgorithm.spec.parse;
import com.tc.dispatch.gsservice.basic.common.regulation;
import com.tc.dispatch.gsservice.basic.common.tcdate;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.OrderSumMain;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.model.combinedOrder;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.tools;

public class orderModifyMaoPi extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orderModifyMaoPi() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public String getKaiMo(String salesid) {
		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		String ret = "";
		try {
			ret = gss.findERPKaiMo(salesid);
		} catch (GSException e) {
			// TODO Auto-generated catch block
			System.out.println("getKaiMo:" + e.getMessage());
			return "";
		}
		return ret;

	}

	
	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();
		OrderSumMain osm = new OrderSumMain();
		OrderSumMain osmDef = new OrderSumMain();
		String pos = "0";
		if (sess.getAttribute("position") != null) {
			pos = (String) sess.getAttribute("position");
		}
		;
		//System.out.println("");
		if (action.equals("default")) {
			if (Integer.parseInt(pos) == 0)// 表示 刚开始
			{
				try {
					osm = (OrderSumMain) sess.getAttribute("OrderSumMain");
					osmDef = (OrderSumMain) sess
							.getAttribute("OrderSumMainDef");

					osm = gss.getERPMaopi(osm);
					// osmDef=gss.getERPMaopi(osmDef);
					// 计算毛中

					float v2 = parse.parseSpec(osmDef.getSpec_yz()).volume();

					float d2 = regulation.getDensity(osmDef.getSsign_yz());

				//	osmDef.setGrossweight(v2 * d2/1000000);
					// 设置未磨成品率，已磨成品率
				//	osmDef.setWeimo(regulation.DEF_WEI_MO_YIELD);
				//	osmDef.setYimo(regulation.DEF_YI_MO_YIELD);
					sess.removeAttribute("OrderSumMain");
					sess.setAttribute("OrderSumMain", osm);

					sess.removeAttribute("OrderSumMainDef");
					sess.setAttribute("OrderSumMainDef", osmDef);
					
					sess.setAttribute("position", "1");

				} catch (GSException e) {
					response.sendRedirect("orderError.jsp?error="
							+ e.getErrorCode());
				}

			} else {
				osm = (OrderSumMain) sess.getAttribute("OrderSumMain");
				osmDef = (OrderSumMain) sess.getAttribute("OrderSumMainDef");
			}

			JSONObject json = new JSONObject();
			JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor());
			cfg.registerJsonValueProcessor(java.sql.Date.class,
					new DateJsonValueProcessor());

			// BeanUtils.copyProperties();
			json.put("totalProperty", new Integer(1));
			List ret = new ArrayList();
			combinedOrder corder = new combinedOrder();
			//corder.setOsm(osm.convertToString());
			//corder.setOsm2(osmDef.convertToString());
			ret.add(corder);

			JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
			json.put("root", arr);

			// json.put("osm", osm);
			response.getWriter().print(json);
		} else if (action.equals("update")) {
			osm = (OrderSumMain) sess.getAttribute("OrderSumMain");
			osmDef = (OrderSumMain) sess.getAttribute("OrderSumMainDef");

			String grossweight = request.getParameter("grossweight");
			String grossweight2 = request.getParameter("grossweight2");
			String yimo = request.getParameter("yimo");
			String yimo2 = request.getParameter("yimo2");
			String weimo = request.getParameter("weimo");
			String weimo2 = request.getParameter("weimo2");
			try {
				//osm.setGrossweight(Float.parseFloat(grossweight));
				//osmDef.setGrossweight(Float.parseFloat(grossweight2));
				//osm.setYimo(Float.parseFloat(yimo));
				//osmDef.setYimo(Float.parseFloat(yimo2));
				//osm.setWeimo(Float.parseFloat(weimo));
				//osmDef.setWeimo(Float.parseFloat(weimo2));
				sess.removeAttribute("OrderSumMain");
				sess.setAttribute("OrderSumMain", osm);

				sess.removeAttribute("OrderSumMainDef");
				sess.setAttribute("OrderSumMainDef", osmDef);
				
				gss.updateOrderSumMain(osm);
				gss.updateOrderSumMain(osmDef);
			} catch (Exception e) {
				out.print("{success:false, data:'false'}");
			}
			out.print("{success:true, data:'sucess'}");

		}

		out.flush();
		out.close();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
